<template>
    <h1>10进制转换64进制</h1>
    <el-form v-model="covert10to64">
        <el-form-item label="转换数字" >
            <el-input v-model="covert10to64.covertNum"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button @click="handleCovert10to64" type="primary"> 10进制 转换 64进制</el-button>
        </el-form-item>

        <el-form-item label="转换结果">
            <el-input v-model="covert10to64.convertResult"> </el-input>
        </el-form-item>
    </el-form>

    <el-divider> </el-divider>
    <h1> 64进制转10进制 </h1>

    <el-form :model="covert64to10">
        <el-form-item label="转换数字" >
            <el-input v-model="covert64to10.covertNum"></el-input>
        </el-form-item>

        <el-form-item>
        <el-button @click="handleCovert64to10" type="primary">64进制 转换 10进制 </el-button>
        </el-form-item>

        <el-form-item label="转换结果">
            <el-input v-model="covert64to10.convertResult"> </el-input>
        </el-form-item>
    </el-form>

</template>

<script>

const MAX_DECIMAL_LENGTH = 52 //小数部分的最大长度
const MIN_DECIMAL = 1e-52

//最高64进制的转换字符映射
const c64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz?_"
function genM64() {
    let m64 = new Map()
    for (let i=0; i< c64.length; i++) {
        m64[c64[i]] = i
    }
    return m64
}

let m64 = genM64()
console.log(m64)


export default {
    name:'convert64',
    
    data() {
        return {
            covert10to64: {
                covertNum: '',
                convertResult: '',
            },
            covert64to10: {
                covertNum: '',
                convertResult: '',
            }

        }
    },
    methods:{
        //10进制整数转64进制整数 num腰围整数
        int10toint64(num) {
            let mods = []
            do{
                let mod = num % 64
                num = Math.floor(num/64)
                mods.unshift(mod)
            }while(num != 0)
            let covertArr = mods.map( v => {
                return c64[v]
            })
            console.log("int convert =>", covertArr)
            return covertArr.join('') 
        },

        //小数转换部分 num为小于1的数
        decimalCovert(num) {
            let result = []
            do {
                console.log(num)
                num = num * 64
                let ret = Math.floor(num)
                result.push(ret)

                num = num - ret
            }while(result.length <= MAX_DECIMAL_LENGTH && num > MIN_DECIMAL)

            let covertArr = result.map( v => {
                return c64[v]
            })
            console.log("decimal convert =>", covertArr)
            return covertArr.join('')
        },

        //64位整数转换成10进制整数
        int64to10(num) {
            let result = 0
            for (let i = 0; i < num.length; i++) {
                let k = num[i]
                let v = m64[k]
                let temp = v * Math.pow(64, num.length-1-i)
                result += temp 
            }
            return result
        },

        decimal64to10(num) {
            let result = 0
            for (let i = 0; i < num.length; i++) {
                let k = num[i]
                let v = m64[k]
                let temp = v * Math.pow(64, -1-i)
                result += temp 
                console.log("[k: ",k, "]; v:",v, "[temp: ",temp, "]; [result: ", result, "]")
            }
            return result 
        },

        //64进程转10进制
        handleCovert64to10() {
            let arr = this.covert64to10.covertNum.split(".")
            console.log(arr)
            let resultInt = this.int64to10(arr[0])
            if (arr.length > 1) {
                let resultDecimal = this.decimal64to10(arr[1])
                this.covert64to10.convertResult = resultInt + resultDecimal
            } else {
                this.covert64to10.convertResult = resultInt
            }
        }
    }
}
</script>